<?php
namespace app\company\controller;

use think\Db;

class Sys extends Base{

    /**
     * 工龄分配置
     */
    public function index(){
        $info = db('com_config')->where('com_id', $this->com_id)->find();
        if(!$info){
            db('com_config')->insert(['com_id'=>$this->com_id]);
            $info = db('com_config')->where('com_id', $this->com_id)->find();
        }
        if(!empty($info['working_age_end'])){
            $info['working_age_end'] = date('Y-m-d H:i', $info['working_age_end']);
        }else{
            $info['working_age_end'] = date('Y-m-d H:i', time());
        }
        $this->assign('info', $info);
        return $this->fetch();
    }
    //工龄分提交
    public function index_edit(){
        $param = input();
        if(empty($param['working_age_end'])){
            return $this->ajaxReturn(1,'启动时间不能为空');
        }
        $param['working_age_end'] = strtotime($param['working_age_end']);
        foreach($param as $k=>$v){
            db('com_config')->where('com_id', $this->com_id)->update([$k=>$v]);
        }
        return $this->ajaxReturn(0, '成功');
    }

    /**
     * 启动分配置
     */
    public function default_point(){
        $info = db('com_config')->where('com_id', $this->com_id)->find();
        if(!$info){
            db('com_config')->insert(['com_id'=>$this->com_id]);
            $info = db('com_config')->where('com_id', $this->com_id)->find();
        }
        $this->assign('info', $info);
        return $this->fetch();
    }
    //启动分修改
    public function default_point_edit(){
        $param = input();
        DB::startTrans();
        try{
            foreach($param as $k=>$v){
                db('com_config')->where('com_id', $this->com_id)->update([$k=>$v]);
            }
            $start_integral = I('start_integral',0);
            //更新用户基础分值
            M('users')->where('com_id',$this->com_id)->update(['base_integral'=>$start_integral]);
            DB::commit();
            return $this->ajaxReturn(0, '成功');
        }catch (\Exception $e){
            DB::rollback();
            return $this->ajaxReturn(1,$e->getMessage());
        }


    }

}